package com.example.service.impl;

import com.example.mapp.DataInterfaceServiceMapp;
import com.example.service.DataInterfaceService;
import com.example.util.ResultUtil;
import com.example.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DataInterfaceServiceImpl implements DataInterfaceService {

    private static final Logger logger = LoggerFactory.getLogger(DemandMaintanceServiceImpl.class);
    @Autowired
    private DataInterfaceServiceMapp dataInterfaceServiceMapp;

    /**
     * 查询获取维保单位
     * @param map
     * @return
     */
    @Override
    public  Map<String, Object> listMaintenancePartyInterface(Map map) {
        Map<String, Object> resultmap = new HashMap<>();
        if (map.get("userName") == null) {
            return ResultUtil.returnSuccess("用户名不能为空，请联系管理员添加", null);
        }
        if(! "XZLUSER".equals(map.get("userName").toString()) ){
            return ResultUtil.returnSuccess("您还没有权限，请联系管理员开启权限", null);
        }
        try {
            List<Map<String, Object>> parammap = dataInterfaceServiceMapp.listMaintenancePartyInterface(map);
            if (parammap != null) {
                for (Map<String, Object> param : parammap) {
                    int maintenanceUnitScore = Integer.parseInt(param.
                            get("maintenanceUnitScore").toString());
                    if (maintenanceUnitScore < 0) {
                        param.put("maintenanceUnitScore", 0);
                    }
                }
            }
            resultmap.put("maintenancePerson", parammap);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败" + e.getMessage(), null);
        }
        return ResultUtil.returnSuccess("查询成功", resultmap);
    }

    /**
     * 获取维保记录
     * @param map
     * @return
     */
    @Override
    public Map<String, Object> listMaintenanceRecordsInterface(Map map) {
        Map<String, Object> resultmap = new HashMap<>();
        if (map.get("userName") == null) {
            return ResultUtil.returnSuccess("用户名不能为空，请联系管理员添加", null);
        }
        if(! "XZLUSER".equals(map.get("userName").toString()) ){
            return ResultUtil.returnSuccess("您还没有权限，请联系管理员开启权限", null);
        }
        if(map.get("startTime") == null || map.get("endTime") == null){
            return ResultUtil.returnSuccess("时间不能为空", null);
        }

        try {
            List<Map<String, Object>> parammap = dataInterfaceServiceMapp.listMaintenanceRecordsInterface(map);
            if(parammap !=null){
                parammap = this.ObjectMap(parammap);
            }
            resultmap.put("halfMaintanceList", parammap);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败" + e.getMessage(), null);
        }
        return ResultUtil.returnSuccess("查询成功", resultmap);
    }

    /**
     * 获取维保项目
     * @param map
     * @return
     */
    @Override
    public Map<String, Object> listMaintenanceProjectInterface(Map map) {

        Map<String, Object> resultmap = new HashMap<>();
        if (map.get("userName") == null) {
            return ResultUtil.returnSuccess("用户名不能为空，请联系管理员添加", null);
        }
        if(! "XZLUSER".equals(map.get("userName").toString()) ){
            return ResultUtil.returnSuccess("您还没有权限，请联系管理员开启权限", null);
        }
        if(map.get("startTime") == null || map.get("endTime") == null){
            return ResultUtil.returnSuccess("时间不能为空", null);
        }
        try {
            // 曳引
            List<Map<String, Object>> parammap = dataInterfaceServiceMapp.listMaintenanceProjectInterface(map);
            if (parammap != null) {
                parammap = this.ObjectMap(parammap);
            }
            // 液压
            List<Map<String, Object>> paraHydp = dataInterfaceServiceMapp.listMaintenanceProjectInterfaceHydpress(map);
            if (paraHydp != null) {
                paraHydp = this.ObjectMap(paraHydp);
                parammap.addAll(paraHydp);
            }
            // 自动
            List<Map<String, Object>> paraEsCalator = dataInterfaceServiceMapp.listMaintenanceProjectInterfaceEsCalator(map);
            if (paraEsCalator != null) {
                paraEsCalator = this.ObjectMap(paraEsCalator);
                parammap.addAll(paraEsCalator);
            }

            // 杂物
            List<Map<String, Object>> paraDebris = dataInterfaceServiceMapp.listMaintenanceProjectInterfaceDebris(map);
            if(paraDebris !=null ){
                paraDebris = this.ObjectMap(paraDebris);
                parammap.addAll(paraDebris);
            }
            resultmap.put("listMaintenanceProject", parammap);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败" + e.getMessage(), null);
        }

        return ResultUtil.returnSuccess("查询成功", resultmap);
    }

    /**
     * 维保标志
     * @param parammap
     * @return
     */
    public List<Map<String, Object>> ObjectMap(List<Map<String, Object>> parammap) {
        if (parammap != null) {
            for (Map<String, Object> intoMap : parammap) {

                String maintenanceProject = "半月保";
                if (intoMap.get("halfMonId") != null) {
                    maintenanceProject = "季度保";
                }
                if (intoMap.get("quarterId") != null) {
                    maintenanceProject = "半年保";
                }
                if (intoMap.get("halfYearId") != null) {
                    maintenanceProject = "年保";
                }
                intoMap.put("maintenanceProject", maintenanceProject);
                intoMap.remove("halfMonId");
                intoMap.remove("quarterId");
                intoMap.remove("halfYearId");
            }
        }
        return parammap;
    }
}
